package control;
import java.awt.Color;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import entities.User;
import GUI.MainFrame;

/**
 * GUI controllers for the user
 * @author Lidor
 *
 */
	public class UserControllers{
		
		public MainFrame mf;
		public User user;
		/**
		 * @param M Instance of the main Frame of the program
		 */
		public UserControllers(MainFrame M){
		mf=M;
		}
		/**
		 * Display the appropriate data into the GUI depending on command
		 * @param msg message received from the DataBase
		 */
		public void handleMessage (Object msg){
			ArrayList<Object> messages=(ArrayList<Object>) msg;
			String command = (String) messages.get(1);
			if(command.equals("UpdateUserCard"))
			{
				if(messages.get(2).equals("OK"))
			{
			
				user=(User)messages.get(3);
				mf.logged_in_user.email=user.email;
				mf.logged_in_user.phone=user.phone;
				System.out.println(mf.logged_in_user.phone);
				mf.userMain.updateUser.textEmail.setText(user.email.toString());
				mf.userMain.updateUser.textPhone.setText(user.phone.toString());
				mf.userMain.updateUser.lblError.setVisible(true);
				JOptionPane.showMessageDialog(null, "Details were successfuly updated!", "Program", JOptionPane.PLAIN_MESSAGE);
			}
			if(messages.get(2).equals("Fail")) 
			{
				mf.userMain.updateUser.lblError.setText("couldn't update user details");
					mf.userMain.updateUser.lblError.setForeground(Color.RED);
					mf.userMain.updateUser.lblError.setVisible(true);
				}
				
			}
			
			if(command.equals("UpdateCardPass"))
			{
				if(messages.get(2).equals("OK"))
			{
			
				user=(User)messages.get(3);
				mf.logged_in_user.email=user.email;
				mf.logged_in_user.phone=user.phone;
				mf.logged_in_user.password=user.password;
				System.out.println(mf.logged_in_user.phone);
				mf.userMain.updateUser.textEmail.setText(user.email.toString());
				mf.userMain.updateUser.textPhone.setText(user.phone.toString());
				mf.userMain.updateUser.lblError.setVisible(true);
				JOptionPane.showMessageDialog(null, "Details were successfuly updated!", "Program", JOptionPane.PLAIN_MESSAGE);
			}
			if(messages.get(2).equals("Fail")) 
			{
				mf.userMain.updateUser.lblError.setText("couldn't update user details");
				mf.userMain.updateUser.lblError.setForeground(Color.RED);
				mf.userMain.updateUser.lblError.setVisible(true);
			}
			if(messages.get(2).equals("PassError")) 
			{
				mf.userMain.updateUser.lblError.setText("not matching passwords");
					mf.userMain.updateUser.lblError.setForeground(Color.RED);
					mf.userMain.updateUser.lblError.setVisible(true);
				}
				
			}
			if(command.equals("ShowReturnDate"))
			{
				if(messages.get(2).equals("OK"))
			{
				mf.userMain.extendBorrow.textReturnDate.setText(messages.get(3).toString());
			}
			else
			{
				mf.userMain.extendBorrow.init();
				JOptionPane.showMessageDialog(null, "There is no active borrow for this copyID!", "Program", JOptionPane.PLAIN_MESSAGE);
				}
			}
			
			if(command.equals("ExtendBorrow"))
			{
				if(messages.get(2).equals("OK"))
			{
				mf.userMain.extendBorrow.init();
				mf.userMain.extendBorrow.textReturnDate.setText(messages.get(3).toString());
				JOptionPane.showMessageDialog(null, "Return date was updated to:"+ messages.get(3).toString(), "Program", JOptionPane.PLAIN_MESSAGE);
			}
			else
			{
				mf.userMain.extendBorrow.init();
				JOptionPane.showMessageDialog(null, "copyID cannot be extended!", "Program", JOptionPane.PLAIN_MESSAGE);
				}
			}
			
			if(messages.get(1).toString().equals("RecentActivity"))
			{
				if (messages.get(2).toString().equals("OK"))
				{
			        String[] activity;
			
			        for(int i=3;i<messages.size();i++)
			        {
			        	activity = (String[]) messages.get(i);
			            String[] activityInfo = {activity[0],activity[1],activity[5],activity[2], activity[3],activity[4] };
			            mf.userMain.recentActivities.searchTableModel.addRow(activityInfo);
			        }
			
				}  
			}
			
			if(messages.get(1).toString().equals("viewErrors"))
			{
				if (messages.get(2).toString().equals("OK"))
				{
			        String[] errors;
			
			        for(int i=3;i<messages.size();i++)
			        {
			        	errors = (String[]) messages.get(i);
			            String[] errorInfo = {errors[0],errors[1],errors[2], errors[3]};
			            mf.userMain.recentActivities.searchTableModel1.addRow(errorInfo);
			        }
			
				}  
			}
			
			if(messages.get(1).toString().equals("viewOrders"))
			{
				if (messages.get(2).toString().equals("OK"))
				{
			        String[] orders;
			
			        for(int i=3;i<messages.size();i++)
			        {
			        	orders = (String[]) messages.get(i);
			            String[] orderInfo = {orders[0],orders[1],orders[2], orders[3]};
			            mf.userMain.recentActivities.searchTableModel2.addRow(orderInfo);
			        }
			
				}  
			}
			
			
			
			if(messages.get(1).toString().equals("Order"))
			{
				if(messages.get(2).equals("OK"))
			{
				mf.userMain.orderBook.init();
				JOptionPane.showMessageDialog(null, "copyID was ordered!", "Program", JOptionPane.PLAIN_MESSAGE);
			}
			else
			{
				mf.userMain.orderBook.init();
				JOptionPane.showMessageDialog(null, "cannot order this copyID!", "Program", JOptionPane.PLAIN_MESSAGE);
				}
			}
			
			if(messages.get(1).toString().equals("ViewBorrowsForUser"))
			{
			if(messages.get(2).equals("OK"))
			{
				String[] borrow;
			
			    for(int i=3;i<messages.size();i++)
			    {
			    	borrow = (String[]) messages.get(i);
			        String[] borrowInfo = {borrow[0],borrow[1],borrow[2],borrow[3]};
					System.out.println(borrow[0]);
					System.out.println(borrow[1]);
			        mf.userMain.viewUser.searchTableModel1.addRow(borrowInfo);
			    }
			}
			}
	return;	
	}
}
			
			
